﻿using System;
using System.Text;
using Newtonsoft.Json;
using System.Net;
using System.Collections.Generic;

namespace AnomalyDetection
{

    internal class APIRequest
    {
        [JsonProperty(PropertyName = "data")]
        public string[,] Series { get; set; }

        [JsonProperty(PropertyName = "params")]
        public Dictionary<string, string> Parameters { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Calling Score API ... ");
            TestScoreAPI();

            Console.WriteLine("\n\n Calling Seasonality Score API ... ");
            TestSeasonalityScoreAPI();

        }



        // Example code to call Score API 
        static void TestScoreAPI()
        {
            // 
            var api = "https://api.datamarket.azure.com/data.ashx/aml_labs/anomalydetection/v2/Score";
            var accountid = "yourid@live.com";
            var accountKey = "Your account key "; // found under My Account -> Account Keys

            var apikey = Convert.ToBase64String(new UTF8Encoding().GetBytes(accountid + ":" + accountKey));

            var inputSeries = new string[,] 
                            {
                            { "06/08/2016 11:10:00 AM", "9" },
                            { "06/08/2016 11:20:00 AM", "12" }
                                        // Add more data points here 
                            };

            var reqParams = new Dictionary<string, string>()
                            {
                                {"tspikedetector.sensitivity", "3"},
                                {"zspikedetector.sensitivity", "3"},
                                {"trenddetector.sensitivity","3"},
                                {"bileveldetector.sensitivity", "3"},
                                {"postprocess.tailRows", "1"}
                            };
            var request = new APIRequest
            {
                Series = inputSeries,
                Parameters = reqParams
            };

            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var authHeader = $"Basic {apikey}";
                client.Headers.Add(HttpRequestHeader.Authorization, authHeader);

                Console.WriteLine("Sending Request...\n " + JsonConvert.SerializeObject(request));
                var jsonBytes = Encoding.Default.GetBytes(JsonConvert.SerializeObject(request));
                var result = client.UploadData(new Uri(api), "POST", jsonBytes);

                var adrecords = ScoreResult.Parse(Encoding.Default.GetString(result));

                foreach (var r in adrecords)
                    Console.WriteLine(r);
                Console.Read();

            }

        }

        // Example code to call ScoreWithSeasonality API
        static void TestSeasonalityScoreAPI()
        {
            var api = "https://api.datamarket.azure.com/data.ashx/aml_labs/anomalydetection/v2/ScoreWithSeasonality";
            var accountid = "yourid@live.com";
            var accountKey = "Your account key "; // found under My Account -> Account Keys
            var apikey = Convert.ToBase64String(new UTF8Encoding().GetBytes(accountid + ":" + accountKey));

            var inputSeries = new string[,]
                            {
                                {"06/08/2016 11:10:00 AM", "9" },
                                {"06/08/2016 11:20:00 AM", "12" }
                                // Add more data points here 
                            };
            var reqParams = new Dictionary<string, string>()
                            {
                                { "preprocess.aggregationInterval","0" },
                                { "preprocess.aggregationFunc", "mean"},
                                {"preprocess.replaceMissing","lkv"},
                                {"postprocess.tailRows", "1"},
                                {"zspikedetector.sensitivity", "3"},
                                {"tspikedetector.sensitivity", "3"},
                                {"upleveldetector.sensitivity", "3.25"},
                                {"bileveldetector.sensitivity", "3.25"},
                                {"trenddetector.sensitivity", "3.25"},
                                {"detectors.historywindow", "500"},
                                {"seasonality.enable","true"},
                                {"seasonality.transform","deSeasonTrend"},
                                {"seasonality.numSeasonality", "1"}
                            };

            var request = new APIRequest
            {
                Series = inputSeries,
                Parameters = reqParams
            };

            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var authHeader = $"Basic {apikey}";
                client.Headers.Add(HttpRequestHeader.Authorization, authHeader);

                Console.WriteLine("Sending Request ...\n " + JsonConvert.SerializeObject(request));
                var jsonBytes = Encoding.Default.GetBytes(JsonConvert.SerializeObject(request));
                var result = client.UploadData(new Uri(api), "POST", jsonBytes);

                var adrecords = ScoreResult.Parse(Encoding.Default.GetString(result), true);

                foreach (var r in adrecords)
                    Console.WriteLine(r);
                Console.Read();

            }

        }

    }
}
